#include<stdio.h>
#include<string.h>
#include<algorithm>
#define M 1000005
using namespace std;
int n,a[M];
long long s[M],f[M],mf,af;
inline void solve(){
	scanf("%d",&n);
	for(int i=1;i<=n;++i) scanf("%d",a+i);
	for(int i=1;i<=n+1;++i) s[i]=s[i-1]+(a[i]==a[i-1]?a[i]:0);
	memset(f,0xc0,sizeof f);
	f[0]=0,mf=0,af=0;
	long long ans=0;
	for(int i=1;i<=n;++i){
		long long tp=af,tp2=mf;
		af+=(a[i]==a[i-1]?a[i]:0),mf+=(a[i]==a[i-1]?a[i]:0);
		f[a[i-1]]=max(tp2,(f[a[i]]+tp)+a[i])-af,mf=max(mf,f[a[i-1]]+af);
		ans=max(ans,mf);
	}
	printf("%lld\n",ans);
}
int main(){
	freopen("color.in","r",stdin);
	freopen("color.out","w",stdout);
	int T;scanf("%d",&T);
	while(T--) solve();
}